//
//  Service+OrderService.m
//  CarService
//
//  Created by fengyn on 15/4/15.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "Services.h"
#import "Service+OrderService.h"

@implementation ServiceManager(OrderService)

///重新支付定单
+ (void)repayOrderWithOrderSn:(NSString *)orderSn
                    payMethod:(NSString *)payMethod
                     password:(NSString *)password
                isIntelligent:(BOOL)isIntelligent
                      success:(void(^)(NSDictionary *data)) success
                      failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/order/callflow/repay";
    if (isIntelligent) {
        path=@"/order/intelligent/repay";
    }
    NSDictionary *parameters = @{@"orderSn":orderSn,
                                 @"payMethod":payMethod,
                                 @"password":password,
                                 };
    [ServiceManager getSubmitTokenSuccess:^(NSDictionary *data) {
        NSString *sToken=(NSString *)data;
        if ([sToken isKindOfClass:[NSString class]]) {
            NSDictionary *headerParameters=@{@"submitToken":sToken};
            [NetworkClient putRequest:path parameters:parameters headerParameters:headerParameters NeedPrompt:YES success:^(id dataObject) {
                success(dataObject);
            } failure:^(id dataObject) {
                failure(dataObject);
            }];
        }
    } failure:^(NSDictionary *data) {
        
    }];
            
}

///提交订单
+ (void)createOrderWithProductType:(NSString *)productType
                            amount:(NSString *)amount
                         payMethod:(NSString *)payMethod
                          products:(NSDictionary *)products
                          password:(NSString *)password
                     phoneLocation:(NSString *)phoneLocation
                       phoneNumber:(NSString *)phoneNumber
                       submitToken:(NSString *)submitToken
                             score:(NSString *)score
                          couponNo:(NSString *)couponNo
                           success:(void(^)(NSDictionary *data)) success
                           failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/order/callflow/call";
    if ([productType isEqualToString:kProductTypeCall]) {
        path = @"/order/callflow/call";
    }else
    if ([productType isEqualToString:kProductTypeIntelligent]) {
        path = @"/order/intelligent";
    }else
    if ([productType isEqualToString:kProductTypeFlow]) {
        path = @"/order/callflow/flow";
    }else
    if ([productType isEqualToString:kProductTypeFlowPackage]) {
        path = @"/order/callflow/flowpackage";
    }
    [ServiceManager getSubmitTokenSuccess:^(NSDictionary *data) {
        NSString *sToken=(NSString *)data;
        if ([sToken isKindOfClass:[NSString class]]) {
            NSDictionary *parameters = @{@"amount":amount,
                                         @"payMethod":payMethod,
                                         @"products":products,
                                         @"password":password,
                                         @"phoneLocation":phoneLocation,
                                         @"OrderPostscript":phoneNumber,
                                         @"score":score,
                                         @"couponNo":couponNo};
            NSDictionary *headerParameters=@{@"submitToken":sToken};
            [NetworkClient postRequest:path parameters:parameters headerParameters:headerParameters needPrompt:YES success:^(id dataObject) {
                success(dataObject);
            } failure:^(id dataObject) {
                failure(dataObject);
            }];
        }
    } failure:^(NSDictionary *data) {
        
    }];
    
    
}

///根据订单支付状态查询订单列表
+ (void)getAllOrdersWithPayStatus:(BOOL)isPay productType:(NSString *)productType pageIndex:(NSUInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/order/callflow/page"];
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"payStatus":@(isPay),
                                  @"orderType":productType,
                                  };
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///创建约考单
+ (void)createReservationWithSubject:(NSString *)subject success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/reservation";
    NSDictionary * dic = @{@"course":subject};
    [NetworkClient postRequest:path parameters:dic NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///获取预约时间段
+ (void)getScheduleTemplateSuccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/scheduletemplate";
    NSDictionary * dic = nil;
    [NetworkClient getRequest:path parameters:dic NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///提交预约练车单
+ (void)createLearningDriveOrderWithStringDateDic:(NSDictionary *)dateDiction subject:(NSString *)subject itemIndex:(NSUInteger)itemIndex regionCode:(NSString *)regionCode address:(NSString *)address success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/order/practice";
    NSString *item=@"";
    NSArray *items=[self getSubjectSecondItems];
    if (subject==kSubjectTypeSecond&&itemIndex<items.count) {
        item=items[itemIndex];
    }
    NSDictionary * dic = @{@"appointmentTime":dateDiction,
                           @"practiceSubject":subject,
                           @"practiceItem":item,
                           @"region":regionCode,
                           @"addressDetail":address};
    [NetworkClient postRequest:path parameters:dic NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///科目二子项目数组
+ (NSArray *)getSubjectSecondItems
{
    return @[@"item1",@"item2",@"item3",@"item4",@"item5"];
}

///根据订单号查询订单详情
+ (void)getOrderDetailWithProducts:(NSString *)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/order/serves/%@",orderSn];
    NSDictionary * dic = nil;
    [NetworkClient getRequest:path parameters:dic NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///支付订单
+ (void)payOrderWithOrderSn:(NSString *)orderSn amount:(NSNumber *)amount payChannel:(NSString *)payChannel cardNo:(NSString *)cardNo success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/payment/charge";
    NSDictionary *parameterDict=@{
                                  @"orderSn":orderSn,
                                  @"channel":payChannel,
                                  @"amount":amount,
                                  @"productAmount":amount,
                                  @"vipCardNo":cardNo,
                                  };
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///报名支付
+ (void)paySignUpWithName:(NSString *)name amount:(NSNumber *)amount idCard:(NSString *)idCard payChannel:(NSString *)payChannel local:(NSString*)local orangeKey:(NSString*)orangeKey uccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/order/registration";
    NSDictionary * parameterDict = @{@"amount":amount,
                                     @"payMethod":payChannel,
                                     @"inviteCode":orangeKey,
                                     @"userName":name,
                                     @"idCard":idCard,
                                     @"dependency":local};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///购买保险
+ (void)payInsuranceWithAmount:(NSString *)amount payChannel:(NSString *)payChannel email:(NSString *)email uccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/order/insure";
    NSDictionary * parameterDict = @{@"amount":amount,
                                     @"payMethod":payChannel,
                                     @"email":email};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];

}
+ (void)createOrderWithProducts:(NSDictionary *)productDic amount:(double)amount payChannel:(NSString *)payChannel success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/order/foods";
    NSDictionary *parameterDict=@{@"products":productDic,
                                  @"amount":@(amount),
                                  @"channel":payChannel};
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///支付完成回调
+ (void)payFinishWithChargeId:(NSString *)chargeId success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = [NSString stringWithFormat:@"/trade/charge/%@/callbacks",chargeId];
    NSDictionary *parameterDict=nil;
    [NetworkClient postRequest:path parameters:parameterDict NeedPrompt:NO success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///获取订单列表(分页)
+ (void)getAllFoodOrdersWithPageIndex:(NSUInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/order/foods/public/";
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}

///查询所有订单
+ (void)getAllOrderWithProducts:(NSString *)orderStatus pageIndex:(NSUInteger)pageIndex success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    if (!orderStatus)
    {
        orderStatus=@"";
    }
    NSString * isComment=@"";
    if (orderStatus == kOrderStatusWaitReview)
    {
        isComment=@"N";
    }
    NSString * path = [NSString stringWithFormat:@"/order/serves/page/public?"];
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"orderStatus":orderStatus,
                                  @"isComment":isComment};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
    
}

///查询约车订单
+ (void)getOrderTrainingCar:(NSString *)workId pageIndex:(NSUInteger)pageIndex orderType:(NSString *)orderType success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/order/subscribe/orderinfo?"];
    const int countPer=20;
    NSDictionary *parameterDict=@{@"start":@(pageIndex*countPer),
                                  @"length":@(countPer),
                                  @"workerId":workId,
                                  @"orderStatus":orderType,
                                  @"orderType":@"PRACTICE"};
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];

}

///取消预约练车单
+ (void)cancelOrderTrainingCar:(NSString *)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = [NSString stringWithFormat:@"/order/practice/%@/public/cancelation",orderSn];
    NSDictionary *parameterDict=nil;
    [NetworkClient putRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];

}

///评价订单
+ (void)postCreateComment:(NSString *)NBR commentBody:(NSString *)commentBody orderSn:(NSString *)orderSn starScore:(NSString *)starScore success:(void(^)(NSDictionary * data)) success failure:(void(^)()) failure
{
    NSString * path = @"/comment";
    NSDictionary * dict = @{@"targetType":@"ORDER",
                            @"targetSequenceNbr":NBR,
                            @"commentBody": commentBody,
                            @"orderSn":orderSn,
                            @"starScore":starScore};
    
    [NetworkClient postRequest:path parameters:dict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];

}

///根据订单号获取订单详情
+ (void)getOrderInfoWithOrderSn:(NSString *)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/order/subscribe/orderinfo/%@",orderSn];
    NSDictionary *parameterDict=nil;
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///教练端根据订单获取详情
+ (void)getCocahOrderInfoWithOrderSn:(NSString *)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/customablitity/score/%@",orderSn];
    NSDictionary * parameterDict = nil;
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///按天查预约单 \param date yyyy-MM-dd
+ (void)getOrdersWithDate:(NSString *)date success:(void (^)(NSDictionary *))success failure:(void (^)(NSDictionary *))failure
{
    NSString * path = [NSString stringWithFormat:@"/order/subscribe/orderinfo"];
    NSDictionary *parameterDict=@{@"start":@0,
                                  @"length":@1000,
                                  @"scheduleDate":date,
                                  };
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///根据订单号查询收益情况
+ (void)getOrderSnFinancialBenefitsWithPageIndex:(NSInteger)pageIndex orderSn:(NSString*)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = @"/funds/profit/public/me";
    const int countPer=20;
    NSDictionary * parameteDict = @{@"start":@(pageIndex*countPer),
                                    @"length":@(countPer),
                                    @"orderSn":orderSn};
    [NetworkClient getRequest:path parameters:parameteDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///根据订单查询理财流量使用详情
+ (void)OrderSnUseFinancialBenefitsWithOrderSn:(NSString*)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/funds/usage/detail/public/%@",orderSn];
    NSDictionary * parameteDict = nil;
    [NetworkClient getRequest:path parameters:parameteDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}

///根据订单号回购理财流量
+ (void)orderSnBuyBackFinancialBenefitsWithOrderSn:(NSString*)orderSn success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString * path = [NSString stringWithFormat:@"/funds/redemption/%@",orderSn];
    NSDictionary * parameteDict = nil;
    [NetworkClient putRequest:path parameters:parameteDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}
@end
